using System.Collections.Generic;
using UnityEngine;
using Verse;
using RimWorld;

namespace RimZombieGirl
{
    // CompProperties_ConsumeLeap
    // CompAbilityEffect_ConsumeLeap
    // Verb_CastAbilityJump
    public class CompAbilityEffect_Bite : CompAbilityEffect, ICompAbilityEffectOnJumpCompleted
    {


        public new CompProperties_Bite Props => (CompProperties_Bite)props;

        public void OnJumpCompleted(IntVec3 origin, LocalTargetInfo target)
        {
            // 获取当前目标
            Pawn pawn = target.Pawn;
            if (pawn == null)
            {
                pawn = GetOriginalTarget().Pawn ?? null;
                if (pawn == null)
                {
#if DEBUG
                    Log.Message("No pawn to bite");
                    // 输出target tostring
#endif
                    return;
                }
            }


            // 撕咬次数
            var chance = Rand.Range(Props.chanceMin, Props.chanceMax);
            for (int i = 0; i < chance; i++)
            {
                if (pawn.Dead)
                {
                    return;
                }
                float num = Rand.Range(Props.damageMin, Props.damageMax);
                DamageInfo dinfo = new DamageInfo(DefDatabase<DamageDef>.GetNamed("Cut"), num, Props.armorPenetration, -1f,
                 parent.pawn, null, null/*这里可能要替换成獠牙*/, DamageInfo.SourceCategory.ThingOrUnknown, null);
                pawn.TakeDamage(dinfo);
                if (pawn.Dead)
                {
                    break;
                }
            }


            var satiety = Props.satiety;
            var targetHediff = Props.targetHediffAfterBite;
            var targetHediffSeverity = Props.targetHediffSeverity;

            // 同派系时
            if (pawn.Faction == parent.pawn.Faction)
            {
                if (Props.satietyFaction >= 0)
                {
                    satiety = Props.satietyFaction;
                }
                if (Props.targetHediffAfterBiteFaction != null)
                {
                    targetHediff = Props.targetHediffAfterBiteFaction;
                }
                if (Props.targetHediffSeverityFaction >= 0)
                {
                    targetHediffSeverity = Props.targetHediffSeverityFaction;
                }
            }

            // 自己添加Hediff
            if (Props.casterHediffAfterBite != null)
            {
                Hediff hediff = HediffMaker.MakeHediff(Props.casterHediffAfterBite, parent.pawn);
                parent.pawn.health.AddHediff(hediff);
            }

            // 自己添加饱食度
            parent.pawn.needs.food.CurLevel += satiety;

            // 目标添加Hediff
            if (targetHediff != null)
            {
                Hediff hediff = HediffMaker.MakeHediff(targetHediff, pawn);
                hediff.Severity = targetHediffSeverity;
                pawn.health.AddHediff(hediff);
            }

        }

        public override bool AICanTargetNow(LocalTargetInfo target)
        {
            return true;
        }

        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
        {
            base.Apply(target, dest);
        }

        public LocalTargetInfo GetOriginalTarget()
        {
            if (parent.verb is Verb_CastAbilityJumpExt jumpVerb)
            {
                return jumpVerb.originalTarget;
            }
            else
            {
                Log.Error("parent.verb is not of type Verb_CastAbilityJumpExt");
                return LocalTargetInfo.Invalid;
            }
        }

    }

}